package com.molyfun.parents.activity.course.view.courseitem;

import android.content.Context;
import android.net.Uri;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;

import com.molyfun.parents.R;
import com.molyfun.parents.activity.common.GameViewerActivity;
import com.molyfun.parents.activity.common.OfflineGameViewerActivity;
import com.molyfun.parents.activity.course.bean.CourseMediaBean;
import com.molyfun.parents.dao.entity.SingleDownloadTask;
import com.molyfun.parents.dao.gen.SingleDownloadTaskDao;
import com.molyfun.parents.service.filedownload.CourseDownLoadUtils;
import com.squareup.picasso.Picasso;

import java.io.File;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by william(liuchao.hit@gmail.com).
 * Date:10/18/16.
 */
public class CourseGameView extends BaseCourseView implements View.OnClickListener{

    protected LayoutInflater layoutInflater;

    private Context context;

    @InjectView(R.id.course_cell_game_img)
    ImageView mCourseCellImg;

    private SingleDownloadTask mSingleCoverDownloadTask, mSingleGameDownloadTask;

    public CourseGameView(Context context, AttributeSet attrs, int defStyle) {
        this(context, attrs);
    }

    public CourseGameView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public CourseGameView(Context context) {
        super(context);
        init(context, null);
    }

    private void init(Context context, AttributeSet attrs) {
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
        layoutInflater.inflate(R.layout.layout_course_cell_game, this);
        ButterKnife.inject(this);

    }

    /**
     * init view
     *
     */
    public void initData(CourseMediaBean courseMediaBean) {
        super.initData(courseMediaBean);
        if(courseMediaBean == null){
            return;
        }
        if(!TextUtils.isEmpty(courseMediaBean.getImgurl())){
            if(mCourseMediaBean.isOffLineMode()){
                //TODO 本地游戏图片
                List<SingleDownloadTask> singleDownloadTasks = singleDownloadTaskDao.queryBuilder().where(SingleDownloadTaskDao.Properties.Url.eq(courseMediaBean.getImgurl())).build().list();
                if(singleDownloadTasks != null && singleDownloadTasks.size() > 0){
                    mSingleCoverDownloadTask = singleDownloadTasks.get(0);
                }
                if(mSingleCoverDownloadTask != null) {
                    if(!TextUtils.isEmpty(mSingleCoverDownloadTask.getPath())){
                        mCourseCellImg.setImageURI(Uri.fromFile(new File(mSingleCoverDownloadTask.getPath())));
                    }else{
                        mCourseCellImg.setImageResource(R.mipmap.ic_game_holder);
                    }
                }else{
                    mCourseCellImg.setImageResource(R.mipmap.ic_game_holder);
                }
            }else{
                Picasso.with(context)
                        .load(courseMediaBean.getImgurl())
                        .placeholder(R.mipmap.ic_game_holder)
                        .into(mCourseCellImg);
            }

        }else{
            mCourseCellImg.setImageResource(R.mipmap.ic_game_holder);
        }

        //update game files
        if(!mCourseMediaBean.isOffLineMode()){
            List<SingleDownloadTask> singleDownloadTasks = singleDownloadTaskDao.queryBuilder().where(SingleDownloadTaskDao.Properties.GameId.eq(mCourseMediaBean.getGameid())).build().list();
            if(singleDownloadTasks.size() > 0 && singleDownloadTasks.get(0) != null) {
                if(!TextUtils.isEmpty(mCourseMediaBean.getOfflineurl()) && !mCourseMediaBean.getOfflineurl().equals(singleDownloadTasks.get(0).getUrl())){
                    CourseDownLoadUtils.updateGameAttachment(context, singleDownloadTasks.get(0), mCourseMediaBean.getOfflineurl());
                }
            }
        }

        mCourseCellImg.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.course_cell_game_img:
                if(mCourseMediaBean.isOffLineMode()){
                    //TODO 本地游戏
                    List<SingleDownloadTask> singleDownloadTasks = singleDownloadTaskDao.queryBuilder().where(SingleDownloadTaskDao.Properties.Url.eq(mCourseMediaBean.getUrl())).build().list();
                    if(singleDownloadTasks != null && singleDownloadTasks.size() > 0){
                        mSingleGameDownloadTask = singleDownloadTasks.get(0);
                    }
                    if(mSingleGameDownloadTask != null) {
                        if(!TextUtils.isEmpty(mSingleGameDownloadTask.getPath())){
                            OfflineGameViewerActivity.startActivity(getContext(), mSingleGameDownloadTask.getPath());
//                            MediaUtils.startVideo(getContext(), mSingleGameDownloadTask.getPath(), mCourseMediaBean.getGalleryname());
                        }
                    }
                }else{
                    GameViewerActivity.startActivity(context, mCourseMediaBean.getUrl());
                }

                break;

        }
    }

}